<?php
namespace App\Services;

use  App\Models\ModuleModel;
use Illuminate\Support\Facades\DB;

class ModuleService
{
    //权限信息列表
    static public function roleModuleList()
    {
        $firstModuleInfo = ModuleModel::select(
                                            'id',
                                            'module_name',
                                            'upper_module_id',
                                            'module_img',
                                            'type',
                                            'sort',
                                        )
                                        ->where('upper_module_id', 0)
                                        ->where('delete_flag', 0)
                                        ->orderBy('sort', 'asc')
                                        ->orderBy('id', 'DESC')
                                        ->get()->toArray();

        $firstModuleId = [];
        foreach($firstModuleInfo as $k => $v) {
            $firstModuleId[] = $v['id'];
        }

        $secondModuleInfo = ModuleModel::select(
                                            'id',
                                            'module_name',
                                            'upper_module_id',
                                            'module_img',
                                            'type',
                                            'sort',
                                        )
                                        ->whereIn('upper_module_id', $firstModuleId)
                                        ->where('delete_flag', 0)
                                        ->orderBy('sort', 'asc')
                                        ->orderBy('id', 'DESC')
                                        ->get()->toArray();

        foreach($firstModuleInfo as $key => $value) {
            foreach($secondModuleInfo as $k => $v) {
                if($value['id'] == $v['upper_module_id']) {
                    $firstModuleInfo[$key]['second_module'][] = $v;
                }
            }
        }

        return $firstModuleInfo;
    }

    //权限列表
    static public function moduleList($data)
    {
        return ModuleModel::select(
                                'id',
                                'module_name',
                                'module_img',
                                'sort',
                                'type',
                            )
                            ->with('secondModule', function($query) {
                                $query->select(
                                        'id',
                                        'module_name',
                                        'module_img',
                                        'sort',
                                        'type',
                                        'upper_module_id',
                                    )
                                    ->orderBy('sort', 'asc')
                                    ->orderBy('id', 'desc')
                                    ->where('delete_flag', 0);
                            })
                            ->where('delete_flag', 0)
                            ->where('upper_module_id', 0)
                            ->orderBy('sort', 'asc')
                            ->orderBy('id', 'desc')
                            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
    }

    //新建模块权限
    static public function add($data)
    {
        if($data['upper_module_id'] != 0) {
            $condition[] = ['upper_module_id', '!=', 0];
        }
        else {
            $condition['upper_module_id'] = ['EQ' => 0];
        }

        $checkModuleInfo = ModuleModel::where([
                                        'module_name' => $data['module_name'],
                                        'delete_flag' => 0
                                    ])
                                    ->where($condition)
                                    ->first();

        if(!empty($checkModuleInfo)) {
            return ['code' =>1, 'msg' => '模块已存在', 'data' => ''];
        }

        $data['created_at'] = time();

        $insertId = ModuleModel::insertGetId($data);

        return ['code' => 0, 'msg' => 'success', 'data' => ['id' => $insertId]];
    }

    //修改
    static public function edit($data)
    {
        if($data['is_parent']) {
            $checkModuleInfo = ModuleModel::where([
                                            'module_name' => $data['module_name'],
                                            'delete_flag' => 0
                                        ])
                                        ->where('id', '!=', $data['id'])
                                        ->first();
        }
        else {
            if(empty($data['upper_module_id'])) {
                return ['code' => 1, 'msg' => '`upper_module_id`不能为空', 'data' => ''];
            }

            $checkModuleInfo = ModuleModel::where([
                                            'module_name' => $data['module_name'],
                                            'delete_flag' => 0,
                                            'upper_module_id' => $data['upper_module_id'],
                                        ])
                                        ->where('id', '!=', $data['id'])
                                        ->first();
        }

        if(!empty($checkModuleInfo)) {
            return ['code' =>1, 'msg' => '模块已存在', 'data' => ''];
        }

        unset($data['is_parent']);

        ModuleModel::where('id', $data['id'])->update($data);
        return ['code' => 0, 'msg' => 'success', 'data' => ''];
    }
}
